package top.wangjiu.java.jvm.constantinfo.refrence;

import top.wangjiu.java.jvm.constantinfo.ConstantPoolInfo;
import top.wangjiu.java.jvm.constantinfo.SymbolicReferenceConstantPoolInfo;

/**
 * @ClassName MethodHandleInfo
 * @Description
 * CONSTANT_MethodHandle_info结构用于表示方法句柄；
 * CONSTANT_MethodHandle_info {
 *     u1 tag;
 *     u1 reference_kind;//reference_kind 项的值必须在 1 至 9 之间（包括 1 和 9），它决定了方法句柄的类型。
 *     方法句柄类型的值表示方法句柄的字节码行为。
 *     u2 reference_index;//reference_index 项的值必须是对常量池的有效索引。
 * }
 * @Author wangjiu
 * @Date 2022/1/14 11:56 上午
 */
public class InvokeDynamicCPInfo extends SymbolicReferenceConstantPoolInfo {

    public final static short TAG = 18;
    public final static short INFO_LENGTH = 4;
    private final short index;

    private final short bootstrapMethodAttrIndex;
    private final short nameAndTypeIndex;

    public InvokeDynamicCPInfo(byte[] bytes, short index){
        this.index = index;
        bootstrapMethodAttrIndex = (short)(bytes[0] << 8 | bytes[1]);
        nameAndTypeIndex = (short)(bytes[2] << 8 | bytes[3]);
    }

    @Override
    public short tag() {
        return TAG;
    }

    @Override
    public short index() {
        return index;
    }

    @Override
    public short infoLength() {
        return INFO_LENGTH;
    }

    @Override
    public String toFullString(ConstantPoolInfo[] poolInfos) {
        return "#"+index +" = InvokeDynamic" + "\t\t#"+bootstrapMethodAttrIndex +".#"+ nameAndTypeIndex+ "\t\t// "+poolInfos[nameAndTypeIndex].toString(poolInfos);
    }

    @Override
    public String toString(ConstantPoolInfo[] poolInfos) {
        return poolInfos[nameAndTypeIndex].toString(poolInfos);
    }
}
